trigger DNC_IDNC_maskingPhoneonLead on Lead (before insert,before update) {
User IntegrationUser=[select profileid from user where username='westintegration@apolloglobalwest.com.wiudev'];//Code added by metasts to get profileId of Integration user
//String AppUserProfileId=String.valueOf(IntegrationUser.profileid); //code added by metasts for getting string value of profileId
 List<Case> caseToCreate=new List<Case>();//added by metasts
    RecordType RecId=[select Id from RecordType where Name='West - Support'];//added by metasts
    String RecTypId=String.valueOf(RecId.Id);//added by metasts
    Boolean UserPermission = False;

    // Below code if Profile Ids are used
    Set<String> ProfileIds = new Set<String>();
   
    for (UserAccessDNC__c UA_DNC : UserAccessDNC__c.getAll().values()){
        ProfileIds.add(UA_DNC.Profile_Ids__c.substring(0, 15));
    } 
    if(!ProfileIds.isEmpty()){
        UserPermission = ProfileIds.contains(UserInfo.getProfileId().substring(0, 15));
    }          

/*    Schema.DescribeSObjectResult d = Schema.SObjectType.Lead;
    Map<String,Schema.RecordTypeInfo> rtMapByName = d.getRecordTypeInfosByName();
    Schema.RecordTypeInfo rtByName =  rtMapByName.get('West - Prospective Student');
    String RecTypeId  = rtByName.getRecordTypeId();*/
    for(Lead A: Trigger.New){
    if(trigger.isUpdate ){
    //add by metasts 
            if(Trigger.oldMap.get(A.Id).Home_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Home_Phone_Status__c == 'DNC' && IntegrationUser.profileid==UserInfo.getProfileId()) {
                If(Trigger.newMap.get(A.Id).Home_Phone__c != Null){
                Case c=new Case(RecordTypeId=RecTypId,AccountId=A.id,Status='New',Origin='Web',Subject='IDNC/DNC occurs',Description='IDNC/DNC',Group__c='Student',Category__c='Admissions',Activity__c='Application',Case_Name__c='PRE Eval');
                              
                caseToCreate.add(c);
                }   
                }
                //end by metasts 
    if(IntegrationUser.profileid!=UserInfo.getProfileId())//code added by metasts for override DNC and IDNC rule for QuickApp flow data
    {
        
        //  Home Phone Masking:
        
            if(Trigger.oldMap.get(A.Id).Home_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Home_Phone_Status__c == 'DNC' ) {
                IF(Trigger.newMap.get(A.Id).Home_Phone__c != Null){A.addError('You cannot change "Home Phone" once it\'s status is marked as DNC!');}    
            } else if(Trigger.oldMap.get(A.Id).Home_Phone_Status__c != 'DNC' && Trigger.newMap.get(A.Id).Home_Phone_Status__c == 'DNC') {
                A.Home_Phone__c='';                           
            } else if(Trigger.oldMap.get(A.Id).Home_Phone_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Home_Phone_Status__c  != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Phone DNC permissions can change "DNC" once it\'s status is marked as DNC!');
                } else {
                    A.Home_Phone__c = A.Home_Phone_Unmasked__c;
                }                
            } else {
                A.Home_Phone_Unmasked__c= A.Home_Phone__c ;
            }  
            
       // Mobile Phone Masking :           
            if(Trigger.oldMap.get(A.Id).Mobile_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Mobile_Phone_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).MobilePhone != Null){A.addError('You cannot change "Mobile Phone" once it\'s status is marked as DNC!');}    
            } else if(Trigger.oldMap.get(A.Id).Mobile_Phone_Status__c!= 'DNC' && Trigger.newMap.get(A.Id).Mobile_Phone_Status__c == 'DNC') {
                A.MobilePhone='';                           
            } else if(Trigger.oldMap.get(A.Id).Mobile_Phone_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Mobile_Phone_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Phone DNC permissions can change "DNC" once it\'s status is marked as DNC!');
                } else {
                    A.MobilePhone = A.Mobile_Phone_Unmasked__c;
                }                
            } else {
                A.Mobile_Phone_Unmasked__c= A.MobilePhone ;
            }  
                
        // Work Phone Masking :           
            if(Trigger.oldMap.get(A.Id).Work_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Work_Phone_Status__c== 'DNC') {
                IF(Trigger.newMap.get(A.Id).Work_Phone__c != Null){A.addError('You cannot change "Work Phone" once it\'s status is marked as DNC!');}    
            } else if(Trigger.oldMap.get(A.Id).Work_Phone_Status__c!= 'DNC' && Trigger.newMap.get(A.Id).Work_Phone_Status__c == 'DNC') {
                A.Work_Phone__c ='';                           
            } else if(Trigger.oldMap.get(A.Id).Work_Phone_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Work_Phone_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Phone DNC permissions can change "DNC" once it\'s status is marked as DNC!');
                } else {
                    A.Work_Phone__c  = A.Work_Phone_Unmasked__c;
                }                
            } else {
                A.Work_Phone_Unmasked__c= A.Work_Phone__c  ;
            }          
               
         // Mother's Phone Masking :           
            if(Trigger.oldMap.get(A.Id).Mother_s_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Phone_Status__c== 'DNC') {
                IF(Trigger.newMap.get(A.Id).Mother_s_Phone_Number__c != Null){A.addError('You cannot change "Mother Phone" once it\'s status is marked as DNC!');}    
            } else if(Trigger.oldMap.get(A.Id).Mother_s_Phone_Status__c!= 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Phone_Status__c == 'DNC') {
                A.Mother_s_Phone_Number__c ='';                           
            } else if(Trigger.oldMap.get(A.Id).Mother_s_Phone_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Mother_s_Phone_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Phone DNC permissions can change "DNC" once it\'s status is marked as DNC!');
                } else {
                    A.Mother_s_Phone_Number__c = A.Mother_s_Phone_Unmasked__c ;
                }                
            } else {
                A.Mother_s_Phone_Unmasked__c = A.Mother_s_Phone_Number__c  ;
            }   
            
          // Father's Phone Masking :           
            if(Trigger.oldMap.get(A.Id).Father_s_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Father_s_Phone_Status__c== 'DNC') {
                IF(Trigger.newMap.get(A.Id).Father_s_Phone_Number__c != Null){A.addError('You cannot change "Father Phone" once it\'s status is marked as DNC!');}    
            } else if(Trigger.oldMap.get(A.Id).Father_s_Phone_Status__c!= 'DNC' && Trigger.newMap.get(A.Id).Father_s_Phone_Status__c == 'DNC') {
                A.Father_s_Phone_Number__c ='';                           
            } else if(Trigger.oldMap.get(A.Id).Father_s_Phone_Status__c  == 'DNC' && Trigger.newMap.get(A.Id).Father_s_Phone_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Phone DNC permissions can change "DNC" once it\'s status is marked as DNC!');
                } else {
                    A.Father_s_Phone_Number__c = A.Father_s_Phone_Unmasked__c ;
                }                
            } else {
                A.Father_s_Phone_Unmasked__c = A.Father_s_Phone_Number__c  ;
            }      
            
            // Marketing Phone Masking :           
            if(Trigger.oldMap.get(A.Id).Marketing_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Marketing_Phone_Status__c== 'DNC') {
                IF(Trigger.newMap.get(A.Id).Marketing_Phone__c!= Null){A.addError('You cannot change "Marketing Phone" once it\'s status is marked as DNC!');}    
            } else if(Trigger.oldMap.get(A.Id).Marketing_Phone_Status__c!= 'DNC' && Trigger.newMap.get(A.Id).Marketing_Phone_Status__c == 'DNC') {
                A.Marketing_Phone__c ='';                           
            } else if(Trigger.oldMap.get(A.Id).Marketing_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Marketing_Phone_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Phone DNC permissions can change "DNC" once it\'s status is marked as DNC!');
                } else {
                    A.Marketing_Phone__c = A.Marketing_Phone_Unmasked__c ;
                }                
            } else {
                A.Marketing_Phone_Unmasked__c = A.Marketing_Phone__c  ;
            }      
            
           // Other Phone Masking :           
            if(Trigger.oldMap.get(A.Id).Other_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Other_Phone__c!= Null){A.addError('You cannot change "Other Phone" once it\'s status is marked as DNC!');}    
            } else if(Trigger.oldMap.get(A.Id).Other_Phone_Status__c!= 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_Status__c == 'DNC') {
                A.Other_Phone__c ='';                           
            } else if(Trigger.oldMap.get(A.Id).Other_Phone_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Phone DNC permissions can change "DNC" once it\'s status is marked as DNC!');
                } else {
                    A.Other_Phone__c= A.Other_Phone_Unmasked__c ;
                }                
            } else {
                A.Other_Phone_Unmasked__c = A.Other_Phone__c  ;
            }   
            
           // Other Phone 2 Masking :           
            if(Trigger.oldMap.get(A.Id).Other_Phone_2_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_2_Status__c == 'DNC') {
                IF(Trigger.newMap.get(A.Id).Other_Phone_2__c!= Null){A.addError('You cannot change "Other Phone 2 once it\'s status is marked as DNC!');}    
            } else if(Trigger.oldMap.get(A.Id).Other_Phone_2_Status__c!= 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_2_Status__c == 'DNC') {
                A.Other_Phone_2__c ='';                           
            } else if(Trigger.oldMap.get(A.Id).Other_Phone_2_Status__c == 'DNC' && Trigger.newMap.get(A.Id).Other_Phone_2_Status__c != 'DNC') {
                if(UserPermission == False){
                    A.addError('Only Users with Phone DNC permissions can change "DNC" once it\'s status is marked as DNC!');
                } else {
                    A.Other_Phone_2__c= A.Other_Phone_2_Unmasked__c ;
                }                
            } else {
                A.Other_Phone_2_Unmasked__c = A.Other_Phone_2__c  ;
            } 
            }   
            insert caseToCreate;//added by metasts                                      
    }
    else if(trigger.isInsert){

        //  Home Phone Masking:
            if(A.Home_Phone_Status__c == 'DNC') {
                    A.Home_Phone_Unmasked__c= A.Home_Phone__c;
                    A.Home_Phone__c='';                           
            } else {
                A.Home_Phone_Unmasked__c= A.Home_Phone__c;
            }  
            
       // Mobile Phone Masking           
            if(A.Mobile_Phone_Status__c == 'DNC') {
                    A.Mobile_Phone_Unmasked__c= A.MobilePhone;
                    A.MobilePhone='';                           
            } else {
                A.Mobile_Phone_Unmasked__c= A.MobilePhone;
            }  
            
       // Work Phone Masking           
            if(A.Work_Phone_Status__c == 'DNC') {
                    A.Work_Phone_Unmasked__c= A.Work_Phone__c;
                    A.Work_Phone__c='';                           
            } else {
                A.Work_Phone_Unmasked__c= A.Work_Phone__c;
            } 
            
       //  Mother's Phone Masking:
            if(A.Mother_s_Phone_Status__c == 'DNC') {
                    A.Mother_s_Phone_Unmasked__c= A.Mother_s_Phone_Number__c;
                    A.Mother_s_Phone_Number__c='';                           
            } else {
                A.Mother_s_Phone_Unmasked__c= A.Mother_s_Phone_Number__c;
            }  
            
       //  Father's Phone Masking:
            if(A.Father_s_Phone_Status__c == 'DNC') {
                    A.Father_s_Phone_Unmasked__c= A.Father_s_Phone_Number__c;
                    A.Father_s_Phone_Number__c='';                           
            } else {
                A.Father_s_Phone_Unmasked__c= A.Father_s_Phone_Number__c;
            }    
            
        //  Marketing Phone Masking:
            if(A.Marketing_Phone_Status__c == 'DNC') {
                    A.Marketing_Phone_Unmasked__c= A.Marketing_Phone__c;
                    A.Marketing_Phone__c='';                           
            } else {
                A.Marketing_Phone_Unmasked__c= A.Marketing_Phone__c;
            }  
            
        //  Other Phone Masking:
            if(A.Other_Phone_Status__c == 'DNC') {
                    A.Other_Phone_Unmasked__c= A.Other_Phone__c;
                    A.Other_Phone__c='';                           
            } else {
                A.Other_Phone_Unmasked__c= A.Other_Phone__c;
            }     
            
         //  Other Phone 2 Masking:
            if(A.Other_Phone_2_Status__c == 'DNC') {
                    A.Other_Phone_2_Unmasked__c= A.Other_Phone_2__c;
                    A.Other_Phone_2__c='';                           
            } else {
                A.Other_Phone_2_Unmasked__c= A.Other_Phone_2__c;
            }                                   
                
    }
    }
}